package com.guojian.jdbc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.guojian.common.config.ConfigContext;

/**
 * 数据源工厂
 * @author GuoJian
 *
 */
public class DataSourceFactory {

	private static final Map<String, DataSource> DATASOURCE_MAP = new ConcurrentHashMap<String, DataSource>();

	public static synchronized DataSource getDataSource(String dataSourceName) {
		return DATASOURCE_MAP.computeIfAbsent(dataSourceName, (key) -> initDataSource(dataSourceName));
	}

	private static DataSource initDataSource(String dataSourceName) {
		DruidDataSource druiDataSource = new DruidDataSource();
		druiDataSource.setUrl(ConfigContext.getProperty(dataSourceName + ".url"));
		druiDataSource.setUsername(ConfigContext.getProperty(dataSourceName + ".username"));
		druiDataSource.setPassword(ConfigContext.getProperty(dataSourceName + ".password"));
		druiDataSource.setMaxOpenPreparedStatements(60);
		druiDataSource.setPoolPreparedStatements(true);
		return druiDataSource;
	}

	public static void main(String[] args) {

	}

}
